package com.lqg.bookLibrary.common.util;

import com.lqg.bookLibrary.common.exception.ServiceException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具包
 * */
public class DateHelper {

    /**
     * 将Date化为String
     * */
    public static String DateToString(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    /**
     * 将String化为Date
     * */
    public static Date StringToDate(String date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date toDate;
        try {
            toDate = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ServiceException("时间格式异常");
        }
        return toDate;
    }

    /**
     * 将Date 转化为yyyy-MM-dd
     * */
    public static String DateFront(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    /**
     * 将Date 转化为HH:mm:ss
     * */
    public static String DateTail(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        return formatter.format(date);
    }

    /**
     * 将时间转为不带空格的String
     * */
    public static String DateToStringNoSpace(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");
        return formatter.format(date);
    }

    /**
     * 返回yyyy-MM-dd
     * */
    public static String DateToStringFront(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }
}
